package cc.core.ccequalizer

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.GridView
import android.widget.TextView

class YuSheVewHolder(var parent: ViewGroup) : IEqDialogViewHolder {
    val contentView =
        LayoutInflater.from(parent.context).inflate(
            R.layout.layout_yushe,
            parent,
            false
        ) as GridView

    val list = EqHolder.getYuShe()
    private val presetAdapter = object : BaseAdapter() {
        val inflater = LayoutInflater.from(contentView.context)
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            return inflater.inflate(R.layout.list_item_yushe, parent, false).also {
                val item = getItem(position)
                (it as TextView).text = item.first
                if (EqHolder.getCurPreset() == item.second) {
                    it.setTextColor(Color.RED)
                } else {
                    it.setTextColor(Color.GRAY)
                }
            }
        }

        override fun getItem(position: Int): Pair<String, Short> {
            return list.get(position)
        }

        override fun getItemId(position: Int): Long {
            return 0
        }

        override fun getCount(): Int {
            return list.size
        }
    }

    fun syncStatus() {
        presetAdapter.notifyDataSetChanged()
    }

    init {
        contentView.numColumns = 3
        contentView.adapter = presetAdapter
        contentView.setOnItemClickListener { parent, view, position, id ->
            EqHolder.setPreset(list.get(position).second)
            presetAdapter.notifyDataSetChanged()
        }
    }

    override fun add() {
        if (contentView.parent == null) {
            parent.addView(contentView)
        }
    }

    override fun getContent(): ViewGroup {
        return contentView
    }
}